/*!
 @header OrderManagementViewController.m
 @author 王 文彬
 @version 1.0.0 2013/01/30 Creation
 Copyright (c) 2013年 mypocketbaby. All rights reserved.
 */

#import "OrderManagementViewController.h"
#import "Helper.h"
#import "Config.h"
#import "OrderListViewController.h"
#import "JMFBase.h"

//  -----------------------类详细介绍-----------------------
//
//  <功能描述>：OrderManagementViewController实现较为简单。向服务器请求统计数据，实现uitableview的代理方法即可。
//
//
//  -----------------------类升级日志-----------------------
//
//  修改人   修改时间        修改描述              版本
//
// <#修改人#> <#修改时间#>    <#修改描述#>          <#版本#>


#define ROW_TITLES     @[_L(@"vc_title_022"), _L(@"vc_title_023"), _L(@"vc_title_024"), _L(@"vc_title_075"), _L(@"vc_title_026"), _L(@"vc_title_027"), _L(@"vc_title_025")]
#define ROW_ICONS      @[@"transaction_icon_001@2x.png", @"transaction_icon_002@2x.png", @"transaction_icon_003@2x.png", @"transaction_icon_011@2x.png", @"transaction_icon_005@2x.png", @"transaction_icon_006@2x.png", @"transaction_icon_004@2x.png"]

@interface OrderManagementViewController ()

@end

@implementation OrderManagementViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithBusinessType:(BusinessType)type
{
    self = [super init];
    if (self) {
        _businessType = type;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //背景
    self.view.backgroundColor = [UIColor whiteColor];
    
    //标题
    if (_businessType == BusinessTypeBuyer) {
        self.title = _L(@"vc_title_089");    //订单管理
    }
    else {
        self.title = _L(@"vc_title_063");    //我的订单
    }
    

    //导航按钮：返回
    UIButton* backButton = [Helper buttonWithStyle:ButtonStyleNavigationReturn];
    [backButton addTarget:self action:@selector(backButtonEvent) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    //初始化数据源
    _dataSource = [[NSMutableArray alloc] initWithObjects:@0, @0, @0, @0, @0, @0, @0, nil];


    //初始化表格
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 350) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    JSONRequest* request = [[JSONRequest alloc] init];
    request.URLString = _businessType == BusinessTypeBuyer ? _JSON_URL_PRODUCT_BUYER : _JSON_URL_PRODUCT_SELLER;
    request.delegate = self;
    request.hudOn = true;
    request.hudTitle = _L(@"title_loading_02");
    [request asynchronous];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        
        //图标
        UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 15, 22, 20)];
        [cell.contentView addSubview:imageView];
        
        //文字
        UILabel* titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 15, 200, 20)];
        titleLabel.font = _SYS_FONT_003;
        [cell.contentView addSubview:titleLabel];
        
        //数字
        UILabel* numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(260, 15, 30, 20)];
        numberLabel.text = @"0";
        numberLabel.textAlignment = UITextAlignmentRight;
        numberLabel.textColor = _SYS_COLOR_006;
        numberLabel.font = _SYS_FONT_003;
        [cell.contentView addSubview:numberLabel];
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    //图标
    NSString* iconFilename = [[NSBundle mainBundle] pathForResource:[ROW_ICONS objectAtIndex:indexPath.row] ofType:nil];
    ((UIImageView *)[cell.contentView.subviews objectAtIndex:0]).image = [UIImage imageWithContentsOfFile:iconFilename];
    
    //文字
    ((UILabel *)[cell.contentView.subviews objectAtIndex:1]).text = [ROW_TITLES objectAtIndex:indexPath.row];
    
    //数字
    ((UILabel *)[cell.contentView.subviews objectAtIndex:2]).text = [NSString stringWithFormat:@"%i", [[_dataSource objectAtIndex:indexPath.row] intValue]];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    
    OrderListType orderListType = indexPath.row;
    if (orderListType == 4) {
        
        orderListType = OrderListTypeRefund;
    }
    else if (orderListType == 5) {
        
        orderListType = OrderListTypeReturn;
    }
    else if (orderListType == 6) {
        
        orderListType = OrderListTypeHistory;
    }
    
    
    OrderListViewController* orderListViewController = [[OrderListViewController alloc] initWithBusinessType:_businessType andOrderListType:orderListType];
    [self.navigationController pushViewController:orderListViewController animated:true];
}


#pragma mark - 自定义事件

/*!
 @abstract 返回按钮事件
 */
- (void)backButtonEvent
{
    [self.navigationController popViewControllerAnimated:true];
}


#pragma mark - JSONDelegate

//
- (void)requestDidLoadJSON:(NSDictionary *)dict
{
    JMFBase* jmf = [[JMFBase alloc] initWithJSONDictionary:dict];
    
    if (jmf.status == JMStatusSucceed) {
        
        NSDictionary* values = jmf.data;

        [_dataSource replaceObjectAtIndex:0 withObject:values[@"waitPayCount"]];
        [_dataSource replaceObjectAtIndex:1 withObject:values[@"waitDeliveryCount"]];
        [_dataSource replaceObjectAtIndex:2 withObject:values[@"waitReceivingCount"]];
        [_dataSource replaceObjectAtIndex:3 withObject:values[@"commentReceivingCount"]];
        [_dataSource replaceObjectAtIndex:4 withObject:values[@"refundReceivingCount"]];
        [_dataSource replaceObjectAtIndex:5 withObject:values[@"returnReceivingCount"]];
        [_dataSource replaceObjectAtIndex:6 withObject:values[@"historySuccessCount"]];
        [_tableView reloadData];
    }
    else {
        
        [Helper easyShowJMFStatus:jmf];
    }
}

//
- (void)requestFailWithNetworkUnavailabe
{
    [Helper showAlertWithTitle:nil andMessage:_L(@"title_networkunavailabe")];
}

@end
